<?php
/**
 * 后台订单表操作模型
 * @author lifw
 */
class MUserPromise extends MY_Model {
	protected $table = 'user_promise';

    public function timesecond($seconds)//将秒时间转换具体时间
    {
        $seconds = (int)$seconds;
        if( $seconds>3600 ){
            if( $seconds>24*3600 ){
                $days        = (int)($seconds/86400);
                $days_num    = $days."天";
                $seconds    = $seconds%86400;//取余
            }
            $hours = intval($seconds/3600);
            $minutes = $seconds%3600;//取余下秒数
            $time = $days_num.$hours."小时".gmstrftime('%M分钟%S秒', $minutes);
        }else{
            $time = gmstrftime('%H小时%M分钟%S秒', $seconds);
        }
        return $time;
    }

    public function getList($param, $user_id,$perPage){
        $this->db->select( 'o.*,(o.return_time - o.create_time) used_second,FROM_UNIXTIME(o.create_time) as create_date,c.name as car_name,c.picture as car_picture, c.price as car_price',false);
        $this->db->distinct();
        $this->db->from($this->table.' o');
        $this->db->join('car c','o.car_id = c.id','INNER');
        $this->db->where('o.user_id',$user_id);
        if ($param['status'] !== 4){
            $this->db->where('o.status', $param['status']);
            $this->db->where('o.pay_status',$param['pay_status']);
        }
        $this->db->limit($perPage,$perPage*($param['page'] - 1));
        $list = $this->db->get()->result();
        foreach ($list as $v){
            $v->used_second = $this->timesecond($v->used_second);
        }
        return $list;
    }

	public function getDetail($oid){
	    $this->db->select('o.*,FROM_UNIXTIME(o.create_time) as create_date,FROM_UNIXTIME(o.pay_time) as pay_date,c.name as car_name,c.picture as car_picture, c.price as car_price',false);
	    $this->db->from($this->table.' o');
	    $this->db->join('car c','o.car_id = c.id','INNER');
	    $this->db->where('o.id',$oid);
	    $query = $this->db->get();
	    return $query->row();
    }

    public function getById($id)
    {
        $query = $this->db->where('id', $id)->get($this->table);
        return $query->row();
    }
}
